﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Simple.Common.Core.Log;

namespace Simple.Common.Core.Http
{
    public class SimpleHttpHelper
    {
        public static string Post(string url, string data, int timeOut = 0, string contentType = "application/x-www-form-urlencoded")
        {
            HttpWebRequest hwRequest;
            HttpWebResponse hwResponse;
            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                if (timeOut > 0)
                    hwRequest.Timeout = timeOut;

                hwRequest.Method = "POST";
                hwRequest.ContentType = contentType;


                byte[] buffer = Encoding.UTF8.GetBytes(data);
                hwRequest.ContentLength = buffer.Length;
                Stream smWrite = hwRequest.GetRequestStream();
                smWrite.Write(buffer, 0, buffer.Length);
                smWrite.Close();

                //get response
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
                strResult = srReader.ReadToEnd();
                srReader.Close();
                hwResponse.Close();
            }
            catch (System.Exception err)
            {
                SimpleLogHelper.Error("HTTP调用异常", err);
                throw err;
            }

            return strResult;
        }

        public static string Get(string url,string data=null,int timeOut = 0, string contentType = "application/x-www-form-urlencoded")
        {
            HttpWebRequest hwRequest;
            HttpWebResponse hwResponse;
            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                if (timeOut > 0)
                    hwRequest.Timeout = timeOut;

                hwRequest.Method = "GET";
                hwRequest.ContentType = contentType;

                if (!string.IsNullOrEmpty(data))
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(data);
                    hwRequest.ContentLength = buffer.Length;
                    Stream smWrite = hwRequest.GetRequestStream();
                    smWrite.Write(buffer, 0, buffer.Length);
                    smWrite.Close();
                }

                //get response
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
                strResult = srReader.ReadToEnd();
                srReader.Close();
                hwResponse.Close();
            }
            catch (System.Exception err)
            {
                SimpleLogHelper.Error("HTTP调用异常", err);
                throw err;
            }

            return strResult;
        }
    }

}
